package com.kirk.cts.core.loader;

import com.kirk.cts.core.constants.SystemConstants;
import com.kirk.cts.core.utils.ImageManager;
import org.apache.log4j.Logger;

import javax.swing.*;
import java.awt.*;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.Locale;
import java.util.MissingResourceException;
import java.util.ResourceBundle;

/**
 * <p> ImageLoader.java </p>
 *
 * @version 1.0.0
 * @Author kirkliu
 * @Date 2020-08-29 22:22
 * @Description 图标加载类
 */
public class ImageLoader {
    /**
     * 图标资源绑定对象
     */
    private static ResourceBundle imageBundle;
    /**
     * 选择的图标资源
     */
    private static Locale imageSelected;
    /**
     * ClassLoader
     */
    private static final ClassLoader WD_CLASS_LOADER;
    protected static Logger logger = Logger.getLogger(ImageLoader.class);

    static {
        WD_CLASS_LOADER = AccessController.doPrivileged(new PrivilegedAction<ClassLoader>() {
            @Override
            public ClassLoader run() {
                return new ClassLoader() {
                    @Override
                    public URL getResource(String name) {
                        try {
                            URL result = super.getResource(name);
                            if (result != null) {
                                return result;
                            }
                            return (new File(name)).toURI().toURL();
                        } catch (MalformedURLException ex) {
                            return null;
                        }
                    }
                };
            }
        });
    }

    /**
     * 设置本地化语言
     */
    public static void setLanguage(Locale imageSelected) {
        logger.info("设置本地化语言:" + imageSelected);
        if (imageSelected == null) {
            imageBundle = ResourceBundle.getBundle(SystemConstants.IMAGE_BUNDLE_NAME, Locale.getDefault(), WD_CLASS_LOADER);
        } else {
            imageBundle = ResourceBundle.getBundle(SystemConstants.IMAGE_BUNDLE_NAME, imageSelected, WD_CLASS_LOADER);
        }
        imageSelected = imageBundle.getLocale();
    }

    /**
     * 取得选择的图标资源类型
     */
    public static Locale getLanguageSelected() {
        return imageSelected;
    }

    /**
     * 根据键，取图标资源
     */
    public static ImageIcon getImageIcon(String key) {
        ImageIcon result = null;
        if (imageBundle != null) {
            try {
                String imagePath = imageBundle.getString(key);
                result = ImageManager.getImageIconByShortName(imagePath);
            } catch (MissingResourceException e) {
                System.out.println(e);
            }
        }
        return result;
    }

    /**
     * 根据键，取图片资源
     */
    public static Image getImage(String key) {
        Image result = null;
        if (imageBundle != null) {
            try {
                String imagePath = imageBundle.getString(key);
                result = ImageManager.getImageByShortName(imagePath);
            } catch (MissingResourceException e) {
                System.out.println(e);
            }
        }
        return result;
    }

    public static String getImagePath(String key) {
        String imagePath = imageBundle.getString(key);
        return ImageManager.getImagePath(imagePath);
    }
}
